﻿using B.S.Traceability.Infrastructure.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B.S.Traceability.Infrastructure.Implment
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        private readonly TRDbContext db;
        public BaseRepository(TRDbContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> Add(T entity)
        {
            db.Set<T>().AddAsync(entity);
            return db.SaveChanges();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> Update(T entity)
        {
            db.Set<T>().Update(entity);
            return db.SaveChanges();
        }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns>返回列表对象</returns>
        public async Task<IQueryable<T>> GetAll()
        {
            return db.Set<T>();
        }

        /// <summary>
        /// 批量真删
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> DeleteRemove(List<T> entity)
        {
            db.Set<T>().RemoveRange(entity);
            return await db.SaveChangesAsync();
        }

        /// <summary>
        /// 单真删删
        /// </summary>
        /// <param name="entitys">实体</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> Delete(T entitys)
        {
            db.Set<T>().Remove(entitys);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="entitys"></param>
        /// <returns></returns>
        public async Task<int> UpdateRanhes(List<T> entitys)
        {
            db.Set<T>().UpdateRange(entitys);
            return await db.SaveChangesAsync();
        }
    }
}
